﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Microsoft.ApplicationBlocks.Data;

/// <summary>
///DBAccess 的摘要说明
/// </summary>
public class DBAccess
{
    public static string _connectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();

    public DBAccess()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    // ExecuteNonQuery
    public static int ExecuteNonQuery(string commandText)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteNonQuery(_connectionString, CommandType.Text, commandText);
    }
    public static int ExecuteNonQuery(string commandText, params SqlParameter[] cmdParameters)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteNonQuery(_connectionString, CommandType.Text, commandText, cmdParameters);
    }
    public static int ExecuteNonQueryProc(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteNonQuery(_connectionString, procName, cmdParameters);
    }
    public static int ExecuteNonQueryProcOut(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteNonQuery(_connectionString, CommandType.StoredProcedure, procName, cmdParameters);
    }

    // ExecuteDataTable
    public static DataTable ExecuteDataTable(string commandText)
    {
        CheckCommandText(commandText);
        return ExecuteDataTable(commandText, null);
    }
    public static DataTable ExecuteDataTable(string commandText, params SqlParameter[] cmdParameters)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteDataTable(_connectionString, CommandType.Text, commandText, cmdParameters);
    }

    // ExecuteDataset
    public static DataSet ExecuteDataset(string commandText)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteDataset(_connectionString, CommandType.Text, commandText);
    }
    public static DataSet ExecuteDataset(string commandText, params SqlParameter[] cmdParameters)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteDataset(_connectionString, CommandType.Text, commandText, cmdParameters);
    }
    public static DataSet ExecuteDatasetProc(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteDataset(_connectionString, procName, cmdParameters);
    }

    // ExecuteReader
    public static SqlDataReader ExecuteReader(string commandText)
    {
        return SqlHelper.ExecuteReader(_connectionString, CommandType.Text, commandText);
    }
    public static SqlDataReader ExecuteReader(string commandText, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteReader(_connectionString, CommandType.Text, commandText, cmdParameters);
    }
    //new add on 2011/11/25
    public static SqlDataReader ExecuteReader(SqlConnection connection, string commandText, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteReader(connection, CommandType.Text, commandText, cmdParameters);
    }
    public static SqlDataReader ExecuteReaderProc(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteReader(_connectionString, procName, cmdParameters);
    }

    // ExecuteScalar
    public static object ExecuteScalar(string commandText)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, commandText);
    }
    public static object ExecuteScalar(string commandText, params SqlParameter[] cmdParameters)
    {
        CheckCommandText(commandText);
        return SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, commandText, cmdParameters);
    }
    public static object ExecuteScalarProc(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteScalar(_connectionString, procName, cmdParameters);
    }

    public static void CheckCommandText(string commandText)
    {
        if ((commandText.ToUpper().IndexOf("UPDATE") >= 0 || commandText.ToUpper().IndexOf("DELETE") >= 0) && commandText.ToUpper().IndexOf("WHERE") < 0)
        {
            throw new Exception("Please double check SQLCommand for where condition!");
        }
        byte[] utf8Space = new byte[] { 0xc2, 0xa0 };
        string strSpace = Encoding.GetEncoding("UTF-8").GetString(utf8Space);
        if (commandText.ToUpper().IndexOf(strSpace) >= 0)
        {
            throw new Exception("Please double check SQLCommand for illegal space!");
        }
    }
}
